"use strict";
/* Controllers */
app.controller("areaWordsTopController", ['$scope', '$location', '$rootScope', '$routeParams', 'areaWordsTopService', function ($scope, $location, $rootScope, $routeParams, areaWordsTopService) {

    var vm = $scope.vm;
    vm.p = {};
    //vm.p.appID = $routeParams.appID; //  获取应用模块的名称

    var errorFn = function (result) {
        alert(result);
    };

    $scope.getAreaWordsTop = function () {
        var vm = $scope.vm;
        var ganjiParam = {};
        var wubaParam = {};
        wubaParam.business = 'wuba';
        ganjiParam.business = 'ganji';
        var ganjiProcFn = function (response) {
            var data = response.data;
            if (data == null) return;
            vm.ganji = {};
            vm.ganji.bj = data.北京;
            vm.ganji.gz = data.广州;
            vm.ganji.sz = data.深圳;
            vm.ganji.sh = data.上海;
            vm.ganji.cd = data.成都;
            vm.ganji.cq = data.重庆;
            vm.ganji.hz = data.杭州;
            vm.ganji.wh = data.武汉;
            vm.ganji.cs = data.长沙;
            vm.ganji.dg = data.东莞;
        };
        areaWordsTopService.getAreaWordsTop(ganjiParam, ganjiProcFn, errorFn);

        var wubaProcFn = function (response) {
            var data = response.data;
            if (data == null) return;
            vm.wuba = {}
            vm.wuba.bj = data.北京
            vm.wuba.gz = data.广州
            vm.wuba.sz = data.深圳
            vm.wuba.sh = data.上海
            vm.wuba.cd = data.成都
            vm.wuba.cq = data.重庆
            vm.wuba.hz = data.杭州
            vm.wuba.wh = data.武汉
            vm.wuba.cs = data.长沙
            vm.wuba.dg = data.东莞


        };
        areaWordsTopService.getAreaWordsTop(wubaParam, wubaProcFn, errorFn);
    };

    $scope.getAreaWordsTop()

    $scope.getAreaWordsTopExcel = function (business) {
        var params = {};
        params.business = business
        $(".downloadExcel").addClass('disabled');
        var procFunc = function (response) {
            $(".downloadExcel").removeClass('disabled');
        }
        areaWordsTopService.getAreaWordsTopExcel(params, business + "_excel.xlsx", procFunc);
    }


}]);

/* Services */
app.service('areaWordsTopService', ['ajaxService', function (ajaxService) {

    this.getAreaWordsTop = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/wordTop/getAreaWordsTop.do", successFunction, errorFunction);
    };

    this.getAreaWordsTopExcel = function (params, fileName, successFunction) {
        ajaxService.AjaxPostFile(params, "/star/download/basic-service/v1/wordTop/getAreaWordsTopExcel.do", fileName, successFunction);
    };

}]);

app.filter('inwordToHref', function () { //可以注入依赖
    return function (item) {
        // console.log(item)
        if (item == null) {
            return "-";
        }

        var urlParam = {};
        if (typeof (item.catename) != 'undefined' && item.catename != '' && item.catename.indexOf("/") >= 0) {
            urlParam.catename = item.catename.replaceAll('/', '|')
        } else {
            urlParam.catename = item.catename
        }
        urlParam.inword = item.inword
        // urlParam.business = item.zpsrc
        urlParam.areaName = item.areaname



        if (typeof (item.areaname) != 'undefined' && item.areaname != '') {
            urlParam.aggType = 'aggByCatename'
        } else if (typeof (item.catename) != 'undefined' && item.catename != '') {
            urlParam.aggType = 'aggByAreaName'
        }

        // console.log("urlParam" + urlParam)

        if (item.zpsrc == '58') {
            var str = '#/star/main/wuba_words_top/' + encodeURIComponent(JSON.stringify(urlParam));
            return str;
        } else if (item.zpsrc == 'ganji') {
            var str = '#/star/main/ganji_words_top/' + encodeURIComponent(JSON.stringify(urlParam));
            return str;
        }
    }
});

//app.filter('inwordHrefFilter', function () {
//    return function (item) {
//        console.log(item)
//        if (value == null) {
//            return "-";
//        }
//        let urlParam = {}
//        urlParam.inword = item.inword
//        urlParam.business = item.zpsrc
//        urlParam.areaname = item.areaname
//        if (item.zpsrc == '58') {
//            var str = '#/star/main/wuba_words_top/' + encodeURIComponent(JSON.stringify(urlParam));
//            return str;
//        } else if (item.zpsrc == 'ganji') {
//            var str = '#/star/main/wuba_words_top/' + encodeURIComponent(JSON.stringify(urlParam));
//            return str;
//        }
//
//    }
//});

// function wubaWordFormat(value, row, index) {
//     return wordFormat(value, 'wuba')
// }

// // function ganjiWordFormat(value, row, index) {
// //     return wordFormat(value, 'ganji')
// // }

// function wordFormat(value, business) {
//     if (value == null) {
//         return "-";
//     }
//     //通过controller来获取Angular应用
//     var appElement = document.querySelector('[ng-controller=index]');
//     //获取$scope变量
//     var $scope = angular.element(appElement).scope();
//     $scope.vm.p.business = business;
//     $scope.vm.p.inword = value;

//     let urlParam = $scope.vm.p
//     if (urlParam.catename != '' && typeof (urlParam.catename) != "undefined" && urlParam.catename.indexOf("/") >= 0) {
//         urlParam.catename = urlParam.catename.replaceAll('/', '|')
//         console.log(urlParam)
//     }
//     // var str='<a href="#/star/main/words_chart' + value + '" target="_blank">' + value + '</a>'    
//     var str = '<a href="#/star/main/words_chart/' + encodeURIComponent(JSON.stringify(urlParam)) + '" target="_blank">' + value + '</a>';
//     return str;

// }

function rnkFormatter(value, row, index) {
    if (value == null) {
        return "新晋TOP词汇";
    }
    // //通过controller来获取Angular应用
    // var appElement = document.querySelector('[ng-controller=index]');
    // //获取$scope变量
    // var $scope = angular.element(appElement).scope();
    // $scope.vm.p.business = business;
    // $scope.vm.p.inword = value;

    // var str='<a href="#/star/main/words_chart' + value + '" target="_blank">' + value + '</a>'   
    if (value > 0) {
        var str = Math.abs(value) + '<span style="color:green" class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>';
        return str;
    } else if (value < 0) {
        var str = Math.abs(value) + '<span style="color:red" class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>';
        return str;
    } else {
        return value
    }

    // var str = '<span class="glyphicon glyphicon-search" aria-hidden="true"></span>搜索';
    // return str;
}

// /** 
//  * param 将要转为URL参数字符串x的对象 
//  * key URL参数字符串的前缀 
//  * encode true/false 是否进行URL编码,默认为true 
//  *  
//  * return URL参数字符串 
//  */
// var urlEncode = function (param, key, encode) {
//     if (param == null) return '';
//     var paramStr = '';
//     var t = typeof (param);
//     if (t == 'string' || t == 'number' || t == 'boolean') {
//         paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
//     } else {
//         for (var i in param) {
//             var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
//             paramStr += urlEncode(param[i], k, encode);
//         }
//     }
//     return paramStr;
// };




